package com.sl.au.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.sl.au.entity.OperateLog;
import com.sl.au.entity.User;
import com.sl.au.repository.OperateLogRepository;
import com.sl.au.service.OperateLogService;
import com.sl.utils.DataConverter;

@Service("operateLogService")
@Transactional
public class OperateLogServiceImpl extends BaseServiceImpl implements OperateLogService {
	@Autowired
	OperateLogRepository operateLogRepository;
	
	
	@Override
	public OperateLogRepository getRepository() {
		
		return this.operateLogRepository;
	}

	@Override
	public void log(User user, String operationType, String content) {
		// TODO Auto-generated method stub
		OperateLog log=new OperateLog();
		log.setContent(content);
		log.setOperateType(operationType);
		log.setUser(user);
		operateLogRepository.save(log);
	}

	@Override
	public List<OperateLog> getLogs(int start, int length, Map<String, String> condition) {
		Page<OperateLog> pages = null;
		String key=condition.get("key");
		String startTime = condition.get("startTime");
		String endTime = condition.get("endTime");
		Date startDate = null;
		Date endDate = null;
		if (StringUtils.hasText(startTime)) {// 判定startTime在去掉空格后是否还有字符
			startDate = DataConverter.toDate(startTime);
		}
		if (StringUtils.hasText(endTime)) {
			endDate = DataConverter.toDate(endTime);
		}
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		if (startDate == null && endDate == null){ 
			pages=operateLogRepository.findLog(key, pageable);
		}else if(startDate == null && endDate != null){
			pages=operateLogRepository.findLog(key, endDate, pageable);
		}else if(startDate != null && endDate == null){
			pages=operateLogRepository.findLog(key,startDate,new Date(), pageable);
		}else if(startDate != null && endDate != null){
			pages=operateLogRepository.findLog(key,startDate,endDate, pageable);
		}
		return pages.getContent();
	}

	@Override
	public long getCount(Map<String, String> condition) {
		String key=condition.get("key");
		String startTime = condition.get("startTime");
		String endTime = condition.get("endTime");
		Date startDate = null;
		Date endDate = null;
		long count=0;
		if (StringUtils.hasText(startTime)) {// 判定startTime在去掉空格后是否还有字符
			startDate = DataConverter.toDate(startTime);
		}
		if (StringUtils.hasText(endTime)) {
			endDate = DataConverter.toDate(endTime);
		}
		if (startDate == null && endDate == null){ 
			count=operateLogRepository.getCount(key);
		}else if(startDate == null && endDate != null){
			count=operateLogRepository.getCount(key, endDate);
		}else if(startDate != null && endDate == null){
			count=operateLogRepository.getCount(key, startDate, new Date());
		}else if(startDate != null && endDate != null){
			count=operateLogRepository.getCount(key, startDate, endDate);
		}
		return count;
	}

	
}
